/*
 * Copyright (c) 2008-2011, Petr Panteleyev
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, 
 *       this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice, 
 *       this list of conditions and the following disclaimer in the documentation 
 *       and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products 
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.panteleyev.gclient;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.panteleyev.photo.IPhoto;

import com.google.gdata.data.Content;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.media.mediarss.MediaThumbnail;
import com.google.gdata.data.photos.ExifTags;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.util.ServiceException;

/**
 *
 * @author Petr Panteleyev
 */
public class GPhoto implements IPhoto {
    private PhotoEntry _entry;
    private String _rawImageUrl;
    
    public String getId() {
        return _entry.getGphotoId();
    }
        
    public GPhoto(PhotoEntry entry) {
        _entry = entry;
        
        Content content = _entry.getContent();
        if (content instanceof MediaContent) {
            _rawImageUrl = ((MediaContent)content).getUri();
        } else {
            _rawImageUrl = "";
        }                
    }
     
    public PhotoEntry getEntry() { return _entry; }
    
    public String getHtmlLinkAsString() { return _entry.getHtmlLink().getHref(); }
    public String getRawImageUrl() { return _rawImageUrl; }
    
    public List<MediaThumbnail> getThumbnails() {
        return _entry.getMediaThumbnails();
    }
    
    public MediaThumbnail getThumbnail(int index) {
        return _entry.getMediaThumbnails().get(index);
    }
    
    public Long getHeight() {
        try {
            return _entry.getHeight(); 
        } catch (ServiceException e) {
            return Long.valueOf(0);
        }
    }
    
    public Long getWidth() {
        try {
            return _entry.getWidth(); 
        } catch (ServiceException e) {
            return Long.valueOf(0);
        }
    }
    
    public Long getSize() {
        try {
            return _entry.getSize();
        } catch (ServiceException e) {
            return Long.valueOf(0);
        }
    }
    
    public ExifTags getExifTags() {
        return _entry.getExifTags();
    }
    
    @Override
    public String getTitle() {
        return _entry.getTitle().toString();
    }
    
    public void setTitle(String title) {
        _entry.setTitle(new PlainTextConstruct(title));
    }
    
    public void setImageFromFile(String path, String mimeType) throws IOException {
        MediaFileSource media = new MediaFileSource(new File(path), mimeType);
        _entry.setMediaSource(media);
    }  
}
